package com.traffic.fuwu.controller;

import com.traffic.fuwu.service.DeviceService;
import com.traffic.fuwu.util.Page;
import com.traffic.fuwu.util.ToolUtil;
import com.traffic.fuwu.util.UtilDate;
import com.traffic.fuwu.vo.VoDevice;
import com.traffic.fuwu.vo.VoStatus;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.*;

/**
 * Created by pikewei on 2017/2/8.
 */
@Controller
@RequestMapping("/device")
public class DeviceController {
    private static final Logger log = LoggerFactory.getLogger(DeviceController.class);
    private static final VoStatus voStatus = new VoStatus();

    @Autowired
    private DeviceService deviceService;

    /**
     * 查询机具信息
     * @param model
     * @param session
     * @param pageIndex
     * @param vehNo
     * @param lineName
     * @param deviceId
     * @param request
     * @param response
     * @param appKey
     * @return
     */
    @RequestMapping(value = "/deviceList")
    @ResponseBody
    public Map<String, Object> deviceList(Model model, HttpSession session,
                                          @RequestParam(value = "pageIndex", required = false) String pageIndex,
                                          @RequestParam(value = "vehNo", required = false) String vehNo,
                                          @RequestParam(value = "lineName", required = false) String lineName,
                                          @RequestParam(value = "deviceId", required = false) String deviceId, HttpServletRequest request,
                                          HttpServletResponse response, String appKey) {

        Map<String, Object> map = new HashMap<>();

        String pageSize = "10";
        if (StringUtils.isEmpty(pageIndex)) {
            pageIndex = "1";
        }
        Page page = new Page();
        page.setPageIndex(Integer.parseInt(pageIndex));
        page.setPageSize(Integer.parseInt(pageSize));
        // 查询数量
        int count = deviceService.countDevice(lineName, vehNo, deviceId, appKey);
        page = ToolUtil.getPage(count, page);
        try {
            if (vehNo != null && vehNo != "") {
                vehNo = URLDecoder.decode(vehNo, "UTF-8");
            }
            if (lineName != null && lineName != "") {
                lineName = URLDecoder.decode(lineName, "UTF-8");
            }
            List<Map> list = deviceService.selectDeviceFy(lineName, vehNo, deviceId, appKey, page.startRow(),
                    page.getPageSize());
            // System.out.println(list.get(0));
            List<VoDevice> listDevice = new ArrayList<>();
            if (list != null && list.size() != 0) {
                for (int i = 0; i < list.size(); i++) {
                    VoDevice voDevice = new VoDevice();
                    String a_carryCode = (String) list.get(i).get("carry_code");
                    String b_deviceId = (String) list.get(i).get("device_id");
                    // 数据库int有null值时后台用Integer接收
                    Integer c_isChan1 = (Integer) list.get(i).get("is_chan");
                    String ischan = c_isChan1 + "";// int转String
                    if (ischan == "1") {
                        String c_isChan = "正向";
                        voDevice.setIsChan(c_isChan);
                    } else if (ischan == "2") {
                        String c_isChan = "反向";
                        voDevice.setIsChan(c_isChan);
                    } else {
                        String c_isChan = "暂无数据";
                        voDevice.setIsChan(c_isChan);
                    }

                    String d_lineName = (String) list.get(i).get("line_name");
                    String e_lineShort = (String) list.get(i).get("line_short");
                    String f_gprsCode = (String) list.get(i).get("gprs_card");
                    int id = (int) list.get(i).get("id");
                    String idTest = id + "";
                    String carryNo = (String) list.get(i).get("carry_no");
                    String lineCode = (String) list.get(i).get("line_code");
                    String deptCode = (String) list.get(i).get("dept_code");
                    String deptName = (String) list.get(i).get("dept_name");
                    String companyCode = (String) list.get(i).get("company_code");
                    String companyName = (String) list.get(i).get("company_name");
                    Date time = (Date) list.get(i).get("create_time");
                    String createTime = UtilDate.getDateFormatter(time);

                    int perAmount = (int) list.get(i).get("per_amount");
                    Double sb = ((double) perAmount) / 100;
                    String g_preMount = String.valueOf(sb);
                    if (StringUtils.isEmpty(idTest)) {
                        voDevice.setId("暂无数据");
                    } else {
                        voDevice.setId(idTest);
                    }
                    if (StringUtils.isEmpty(carryNo)) {
                        voDevice.setCarryNo("暂无数据");
                    } else {
                        voDevice.setCarryNo(carryNo);
                    }
                    if (StringUtils.isEmpty(lineCode)) {
                        voDevice.setLineCode("暂无数据");
                    } else {
                        voDevice.setLineCode(lineCode);
                    }
                    if (StringUtils.isEmpty(deptCode)) {
                        voDevice.setDeptCode("暂无数据");
                    } else {
                        voDevice.setDeptCode(deptCode);
                    }
                    if (StringUtils.isEmpty(deptName)) {
                        voDevice.setDeptName("暂无数据");
                    } else {
                        voDevice.setDeptName(deptName);
                    }
                    if (StringUtils.isEmpty(companyCode)) {
                        voDevice.setCompanyCode("暂无数据");
                    } else {
                        voDevice.setCompanyCode(companyCode);
                    }
                    if (StringUtils.isEmpty(companyName)) {
                        voDevice.setCompanyName("暂无数据");
                    } else {
                        voDevice.setCompanyName(companyName);
                    }
                    if (StringUtils.isEmpty(createTime)) {
                        voDevice.setCreateTime("暂无数据");
                    } else {
                        voDevice.setCreateTime(createTime);
                    }
                    if (StringUtils.isEmpty(a_carryCode)) {
                        voDevice.setCarryCode("暂无数据");
                    } else {
                        voDevice.setCarryCode(a_carryCode);
                    }

                    if (StringUtils.isEmpty(b_deviceId)) {
                        voDevice.setDeviceId("暂无数据");
                    } else {
                        voDevice.setDeviceId(b_deviceId);
                    }

                    if (StringUtils.isEmpty(d_lineName)) {
                        voDevice.setLineName("暂无数据");
                    } else {
                        voDevice.setLineName(d_lineName);
                    }

                    if (StringUtils.isEmpty(e_lineShort)) {
                        voDevice.setLineShort("暂无数据");
                    } else {
                        voDevice.setLineShort(e_lineShort);
                    }

                    if (StringUtils.isEmpty(f_gprsCode)) {
                        voDevice.setGprsCode("暂无数据");
                    } else {
                        voDevice.setGprsCode(f_gprsCode);
                    }
                    voDevice.setPreMount(g_preMount);
                    listDevice.add(voDevice);
                }
                VoDevice head = new VoDevice();
                head.setDeviceId("设备号");
                head.setCarryCode("车牌号");
                head.setGprsCode("GPRS编号");
                head.setIsChan("正反向");
                head.setPreMount("单笔金额（元）");
                head.setLineName("线路名");
                head.setLineShort("线路简称");
                /*
                 * String[] head = new String[] { "车牌号", "设备号", "正反向", "线路名",
				 * "线路简称", "GPRS编号", "单笔金额（元）" };
				 */
                map.put("head", head);
                map.put("line", listDevice);
                map.put("page", page);
                voStatus.setCode(1);
                voStatus.setMessage("查询成功");
                map.put("status", voStatus);
                return map;
            }
            map.put("head", "没有查询到相关数据");
            voStatus.setCode(0);
            voStatus.setMessage("没有查询到相关数据");
            map.put("status", voStatus);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("接口异常");
            voStatus.setCode(3);
            voStatus.setMessage("接口异常");
            map.put("status", voStatus);
            return map;
        }

    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteDevice")
    @ResponseBody
    public Map<String, Object> deleteDevice(String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            do {
                if (!StringUtils.isNotBlank(id)) {
                    voStatus.setCode(0);
                    voStatus.setMessage("删除失败");
                    map.put("status", voStatus);
                    break;
                }
                deviceService.deleteDevice(Integer.parseInt(id));
                voStatus.setCode(1);
                voStatus.setMessage("删除成功");
                map.put("status", voStatus);
                break;
            } while (false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}
